﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Smled
{
    public class INIFile
    {
        StreamReader s = null;
        FileStream fs = null;
        List<INIGroup> groups = null;
        bool immediate = false;
        int pos = 0;

        public INIFile(FileStream fStream)
        {
            s = new StreamReader(fStream);
            fs = fStream;
        }

        public INIFile(String fileName)
        {
            s = new StreamReader(fileName);
        }

        public INIFile(FileStream fStream, bool Immediate)
        {
            s = new StreamReader(fStream);
            if (Immediate) loadAll();
        }

        public INIFile(String fileName, bool Immediate)
        {
            s = new StreamReader(fileName);
            if (Immediate) loadAll();
        }

        private void loadAll()
        {
            INIGroup g = null;
            groups = new List<INIGroup>();
            while((g = getNext()) != null)
                groups.Add(g);
            pos = 0;
            s.Close();
            if (fs != null) fs.Close();
            immediate = true;
        }

        public INIGroup getNext()
        {
            if (immediate)
            {
                if (pos < groups.Count)
                    return groups[pos++];
                else
                    return null;
            }
            else
            {
                INIGroup g = null;

                try
                {
                    g = new INIGroup(s);
                }
                catch (Exception)
                {
                    g = null;
                }

                return g;
            }
        }
    }
}